package com.example.myapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.io.File;

public class MyActivity extends Activity {
    private TextView take_phone_textview;
    private ImageView take_phone_imageview;
    private RelativeLayout rela;
    //弹窗
    private  PhotoPopuwindow photoPopuwindow;
    private  Context mContext;
    private File cacheFile;// 拍照后的照片
    private final int REQUEST_CODE_SELECT_PICTURE = 200;// 选择图片
    private final int REQUEST_CODE_TAKE_PHOTO = 201;// 拍照
    private final int REQUEST_CODE_CROP_IMAGE = 202;// 裁剪图片
    //显示图片的配置
    DisplayImageOptions options ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext=this;
        rela= (RelativeLayout) findViewById(R.id.rela);
        take_phone_textview= (TextView) findViewById(R.id.take_phone_textview);
        take_phone_textview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                photoPopuwindow= new PhotoPopuwindow(mContext, rela, new DopostCallback() {
                    @Override
                    public void callback(String t) {
                        if("1".equals(t)){
                            // 拍照
                            if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                                Toast.makeText(mContext, "SD卡当前不可用,请检查后重试...", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            cacheFile = new File(mContext.getExternalCacheDir().getAbsolutePath(), System.currentTimeMillis() + ".jpg");

                            Intent in = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            in.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cacheFile));
                            startActivityForResult(in, REQUEST_CODE_TAKE_PHOTO);
                        }if("2".equals(t)){
                            //读取相册的文件
                            // 选择相册
                            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            startActivityForResult(intent, REQUEST_CODE_SELECT_PICTURE);
                        }
                    }

                    @Override
                    public void error(Exception e) {

                    }
                });
            }
        });
        take_phone_imageview= (ImageView) findViewById(R.id.take_phone_imageview);
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.fg_person_icon)
                .showImageOnFail(R.drawable.fg_person_icon)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    }





    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_CANCELED)
            return;

        switch (requestCode) {
            case REQUEST_CODE_SELECT_PICTURE:// 选择相册图片
                if (data != null) {
                    Uri imageUri = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver().query(imageUri, filePathColumn, null, null, null);
                    if (cursor == null) {
                        return;
                    }
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String avatar_path = cursor.getString(columnIndex);
                    cursor.close();
                    Intent intent = new Intent(mContext, CompleteInfomationPhotoEditActivity.class);
                    intent.putExtra("imagePath", avatar_path);
//                    intent.putExtra("url", "http://api.laoxiangbang.com.cn/index.php?m=Home&c=Api&a=uploadAvatar");
                    startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);
                }
                break;
            case REQUEST_CODE_TAKE_PHOTO:// 拍照
                if (!cacheFile.exists()) {
                    Toast.makeText(mContext, "未捕获拍照的图像...", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(mContext, CompleteInfomationPhotoEditActivity.class);
                intent.putExtra("imagePath", Uri.fromFile(cacheFile).getPath());
//                intent.putExtra("url", "http://api.laoxiangbang.com.cn/index.php?m=Home&c=Api&a=uploadAvatar");

                startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);

                break;
            case REQUEST_CODE_CROP_IMAGE:// 剪裁图片
                String avatar_path = data.getStringExtra("avatar_path");
                take_phone_imageview.setImageURI(Uri.parse(avatar_path));
                break;
            default:
                break;

        }

    }
}
